package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.permission.CreatePermissionResourceBo;
import com.zhuss.bean.bo.core.permission.DeletePermissionResourceBo;
import com.zhuss.bean.bo.core.resource.ResourcePermissionBindBo;
import com.zhuss.bean.vo.core.resource.DetailResourceVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.ResourceDao;
import com.zhuss.core.db.entity.Resource;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.PermissionResourceDeleteType;
import com.zhuss.exception.OceanException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * 绑定菜单资源 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("resourcePermissionBind")
public class ResourcePermissionBind extends AbstractProcessor<Object, ResourcePermissionBindBo> implements Processor<Object, ResourcePermissionBindBo> {

    @Autowired
    private ResourceDao resourceDao;
    
    @Autowired
	@Qualifier("resourceRead")
	private ResourceRead resourceRead;
    
    @Autowired
	@Qualifier("permissionResourceRead")
	private PermissionResourceRead permissionResourceRead;
    
    @Autowired
	@Qualifier("permissionResourceDelete")
	private Processor<Object, DeletePermissionResourceBo> permissionResourceDelete;
    
    @Autowired
	@Qualifier("permissionResourceCreate")
	private Processor<Object, CreatePermissionResourceBo> permissionResourceCreate; 
    
    
    /**
     * 校验
     */
    @Override
    public void check(ResourcePermissionBindBo bo) throws OceanException {
    	Long resourceId = bo.getResourceId();
    	List<Resource> sonList = resourceRead.listSon(resourceId);
    	if (!sonList.isEmpty()) {
    		throw new OceanException(ExceptionCode.不能给父级资源绑定权限);
    	}
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(ResourcePermissionBindBo bo) throws OceanException {
    	
    	Long resourceId = bo.getResourceId();
    	List<Long> paramList = bo.getPermissionIdList();
    	@SuppressWarnings("unchecked")
		List<Long> dbList = (List<Long>)permissionResourceRead.listPermissionId(resourceId);
    	List<Long> addList = new ArrayList<Long>(paramList);
    	List<Long> removeList = new ArrayList<Long>(dbList);
    	
    	removeList.removeAll(paramList);
    	addList.removeAll(dbList);
    	
    	if (!removeList.isEmpty()) {
    		DeletePermissionResourceBo param = new DeletePermissionResourceBo();
    		param.setType(PermissionResourceDeleteType.根据资源删除权限资源关系.key());
    		param.setResourceId(resourceId);
    		param.setPermissionIdList(removeList);
    		
    		param.setModifierId(bo.getConcurrentUserId());
    		param.setModifier(bo.getConcurrentUserName());
    		param.setModifyTime(new Date());
    		permissionResourceDelete.process(param);
    	}
    	
    	for (Long permissionId : addList) {
    		CreatePermissionResourceBo param = new CreatePermissionResourceBo();
    		BeanUtils.copyProperties(bo, param);
    		param.setPermissionId(permissionId);
    		param.setResourceId(resourceId);
    		permissionResourceCreate.process(param);
    	}
    	
    	String name = ((DetailResourceVo) resourceRead.detail(resourceId)).getName();
    	bo.setName(name);
    	bo.setAddList(addList);
    	bo.setRemoveList(removeList);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(ResourcePermissionBindBo param) throws OceanException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.菜单资源绑定.key());
    	record.setTargetId(param.getResourceId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
		return null;
	}
}
